package com.yami.shop.multishop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Tag;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.TagService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @GetMapping("/page")
    @Operation(summary = "根据参数分页获取广场标签列表", description = "根据参数分页获取广场标签列表")
    public ServerResponseEntity<IPage<Tag>> page(PageParam<Tag> page, Tag blog) {
        LambdaQueryWrapper<Tag> tagQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(blog.getStatus()) && blog.getStatus() != 0) {
            tagQueryWrapper.eq(Tag::getStatus, blog.getStatus());
        } else {
            tagQueryWrapper.ne(Tag::getStatus, 0);
        }
        PageParam<Tag> tagList = tagService.page(page, tagQueryWrapper);
        return ServerResponseEntity.success(tagList);
    }

    @PostMapping
    @Operation(summary = "新增广场标签", description = "新增广场标签")
    public ServerResponseEntity<Void> saveTag(@RequestBody Tag tag) {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content", tag.getContent());
        queryWrapper.eq("status", 1);
        Tag tempTag = tagService.getOne(queryWrapper);
        if (tempTag != null) {
            return ServerResponseEntity.showFailMsg("该标签已存在");
        }
        tagService.save(tag);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "修改广场标签", description = "修改广场标签")
    public ServerResponseEntity<Void> updateTag(@RequestBody Tag tag) {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content", tag.getContent());
        queryWrapper.ne("uid", tag.getUid());
        queryWrapper.eq("status", 1);
        Tag tempTag = tagService.getOne(queryWrapper);
        if (tempTag != null) {
            return ServerResponseEntity.showFailMsg("该标签已存在");
        }
        tagService.updateById(tag);
        return ServerResponseEntity.success();
    }
}
